<template>
	<view class="goodwrap">
		<!-- 商品详情 -->
		<!-- 商品信息 -->
		<good-basic :basicdata='gooddata.goodbasic' :isGoods="true" v-on:gofree="gofreeA"></good-basic>

		<!-- 优惠卷 -->
		<coupon :list="gooddata.discount" ref="coupup" v-show="hascoupup"></coupon>

		<!-- 选项卡 -->
		<end-title :endTitle="endTitle"></end-title>

		<!-- tab -->
		<good-action v-on:addCart="addCart" v-on:nowBuy="nowBuy"></good-action>

		<specfic :popShow="hasPopup" v-if="isPopup" :specficdata='gooddata.specficlist' v-on:listenClosePopup="listenClosePopup"
		 v-on:listenTochildEvent="listenTochildEventspecfic" v-on:listenConfirmGoods="listenConfirmGoods"></specfic>


	</view>
</template>

<script>
	import goodBasic from '@/components/goods/good-basic.vue'
	import coupon from '@/components/goods/good-coupon.vue'
	import goodAction from '@/components/goods/good-action.vue'
	import endTitle from "@/components/goods/end-title.vue"
	import specfic from "@/components/goods/specfic.vue"
	import json from '@/json/good.js'
	import json1 from '@/json/cart.js'
	export default {
		components: {
			goodBasic,
			goodAction,
			coupon,
			endTitle,
			specfic

		},
		data() {
			return {
				gooddata: {},
				goodobj: {},
				coupup: '',
				hascoupup: false,
				hasPopup: false,
				isPopup: true,

				endTitle: ["详情", "评价"],

			}
		},
		onLoad(option) {
			// console.log(mockData.data.goodbasic)
			let goodsdata = json.data

			if (option.text && option.src) {
				goodsdata.goodbasic.goodName = option.text
				if (option.oldprice || option.newprice) {
					goodsdata.goodbasic.goodPrice = option.newprice
					goodsdata.goodbasic.goodOldPrice = option.oldprice
				} else {
					goodsdata.goodbasic.goodPrice = 416
					goodsdata.goodbasic.goodOldPrice = 900
				}
				goodsdata.goodbasic.goodImgList.map((item, index) => {
					item.url = option.src
				})
				this.gooddata = goodsdata
			} else {

				goodsdata.goodbasic.goodImgList.map((item, index) => {
					item.url =
						'https://img.alicdn.com/imgextra/i1/2222906716/O1CN01FBb6AD1zTyZtBIKhD_!!0-item_pic.jpg_2200x2200Q50s50.jpg_.webp'
				})
				this.gooddata = goodsdata
			}
		},
		onBackPress(e) {
			// #ifdef APP-PLUS
			if (this.hascoupup === true) {
				this.$refs.coupup.togglePopup('');
				return true;
			}
			// #endif
		},
		methods: {

			// 监听规格，尺寸，数量
			listenTochildEventspecfic(val) {
				let newdate = new Date().getTime()
				this.goodobj = {
					goods_image: this.gooddata.goodbasic.goodImgList[0].url,
					goods_name: this.gooddata.goodbasic.goodName,
					goods_spec: val.measure + '码',
					goods_discount: this.gooddata.goodbasic.goodPrice === '免费' ? '0' : this.gooddata.goodbasic.goodPrice,
					goods_price: this.gooddata.goodbasic.goodOldPrice,
					checked: false,
					number: val.number,
					id: String(newdate)
				}
				// console.log(val,this.goodobj)
			},
			listenClosePopup() {
				this.hasPopup = false;
				this.isPopup = false;
				this.$nextTick(function(){
					this.isPopup = true
				})
			},
			// 加入购物车
			addCart() {

				// if(uni.getStorageSync('hasLogin') === 'true'){
				// 	uni.showToast({
				// 		icon:'none',
				// 		title:'成功加入购物车'
				// 	})
				// 	json1.cartList.push(this.goodobj)
				// }else{
				// 	uni.showToast({
				// 		icon:'none',
				// 		title:''
				// 	})
				// }
				// console.log(this.goodobj)
			},
			// 立即购买

			nowBuy(){
				this.hasPopup = true;
			},
			listenConfirmGoods(){
				uni.navigateTo({
					url:"/pages/order/confirm/confirm"
				})
			},
			// nowBuy(){
			// 	if(uni.getStorageSync('hasLogin') === 'true'){
			// 		let buyarray = [];
			// 		let buyjson = {}
			// 		buyjson = {
			// 			state: 1,
			// 			image: this.goodobj.goods_image,
			// 			title: this.goodobj.goods_name,
			// 			size: this.goodobj.goods_spec,
			// 			amount: this.goodobj.number,
			// 			money: this.goodobj.goods_discount,
			// 		}
			// 		buyarray.push(buyjson)
			// 		this.$jump('/pages/order/ordersure?buy='+JSON.stringify(buyarray))
			// 		// console.log(buyarray)
			// 	}else{
			// 		uni.showToast({
			// 			icon:'none',
			// 			title:'请先登录才能购买'
			// 		})
			// 	}
			// },
			
			// 子组件传过来的事件，领取优惠券
			gofreeA() {
				this.hascoupup = true
				this.$refs.coupup.togglePopup('typecoupup');
				// console.log('点击领取')
			}
		}
	}
</script>

<style lang="scss">
	.goodwrap {
		width: 100%;
		height: 100%;
		padding-bottom: 100rpx;
	}

	page {
		background-color: #F7F7F7;
	}
</style>
